package com.example.itextpdf;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.font.FontProvider;

import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author wwl
 * @version 1.0
 * @date 2024/3/12 10:59
 * @description:
 */
public class Html2PDF {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.append("<!DOCTYPE html>" +
                "<html>" +
                "<head>" +
                "<meta charset=\"UTF-8\">" +
                "<title></title>" +
                "<meta name=\"viewport\" content=\"width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no\">" +
                "</head>" +
                "<body>" +
                "<div style=\"width: 100%; margin: auto;\">" +
                "<div class=\"logo\">" +
                "<img  src=\"" + "https://cdn.jegotrip.com.cn/static/images/logo_bill.png" + "\" style=\"width:80%;\" />" +
                "</div>" +
                "<div class=\"address\" style=\"font-size: 14px; border-bottom: 1px solid #928b8b; padding-bottom: 30px; width: 90%; margin-left: 10px;\">" +
                "<div style=\"font-weight: bold; margin-bottom: 5px;\">中国移动国际有限公司</div>" +
                "<div style=\"margin-bottom: 5px;\">香港新界葵涌葵昌路51号九龙贸易中心1座30楼</div>" +
                "<div style=\"margin-bottom: 5px;\">24小时客户服务热线：+86 13800100186</div>" +
                "<div>官网网址：www.jegotrip.com</div>" +
                "</div>" +
                "<div class=\"electronic-billing\" style=\"margin-top: 10px;\">" +
                "<div class=\"title\" style=\"margin-bottom: 50px;\">" +
                "<div style=\"font-size: 24px; font-weight: bold; text-align: center; margin-bottom: 10px;\">中国移动国际有限公司</div>" +
                "<div style=\"font-size: 24px; font-weight: bold; text-align: center;\">无忧行电子账单</div>" +
                "</div>" +
                "<div class=\"list\">");
        sb.append("<ul style=\"list-style: none;font-size: 14px; border: 1px solid #000000; padding: 0;margin-bottom: 0;border-bottom: 0;\">");
        sb.append("<li style=\" height: 42px;\">" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 30%;padding-left: 5px;font-weight: bold;\">订单编号</span>" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 40%;\">" + "123" + "</span>" +
                "<span ></span>" +
                "</li>");
        sb.append("<li style=\" height: 42px;\">" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 30%;padding-left: 5px;font-weight: bold;\">付款日期</span>" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 40%;\">" + "2017-02-08" + "</span>" +
                "<span ></span>" +
                "</li>");
        sb.append("<li style=\" height: 42px;\">" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 30%;padding-left: 5px;font-weight: bold;\">购买手机号</span>" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 40%;\">" + "15361832766" + "</span>" +
                "<span ></span>" +
                "</li>");
        sb.append("<li style=\" height: 42px;\">" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 30%;padding-left: 5px;font-weight: bold;\">使用手机号</span>" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 40%;\">" + "15361832766" + "</span>" +
                "<span ></span>" +
                "</li>");
        sb.append("<li style=\" height: 42px;\">" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 30%;padding-left: 5px;font-weight: bold;\">产品订购渠道</span>" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 40%;\">" + "产品订购渠道" + "</span>" +
                "<span ></span>" +
                "</li>");
        sb.append("<li>" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 30%;padding-left: 5px;font-weight: bold;\">产品支付渠道</span>" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 40%;\">" + "渠道" + "</span>" +
                "<span ></span>" +
                "</li>" +
                "</ul>");
        sb.append("<ul style=\"list-style: none;font-size: 14px; border: 1px solid #000000; padding: 0;margin-top: 0;\">" +
                "<li style=\"border-bottom: 1px solid #000; height: 42px;\">" +
                "<span  style=\"display: block;text-align: center;line-height: 42px; font-weight: bold;\">商品信息</span>" +
                "</li>" +
                "<li style=\" height: 42px;\">" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 70%;padding-left: 5px;font-weight: bold;padding-right: 6px;\">商品名称</span>" +
                "<span  style=\"width: 25%;display: inline-block;text-align: center;font-weight: bold;\">价格</span>" +
                "</li>" +
                "<li style=\"border-bottom: 1px solid #000; height: 42px;\">" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 70%;padding-left: 5px;padding-right: 6px;\">" + "名称" + "</span>" +
                "<span  style=\"width: 25%;display: inline-block;text-align: center;\">CNY " + 1000 / 100 + "</span>" +
                "</li>" +
                "<li style=\" height: 42px;\">" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 30%;font-weight: bold;\">&nbsp;</span>" +
                "<span  style=\"display: inline-block;line-height: 42px;width: 40%;padding-right: 6px; text-align: right;\">总计</span>" +
                "<span  style=\"width: 25%;display: inline-block;text-align: center;\">CNY " + 10000 / 100 + "</span>" +
                "</li>" +
                "</ul>" +
                "</div>");
        sb.append("<div class=\"remark\" style=\"text-align: center;font-size: 10px;\"><i>*此账单由系统自动生成*</i></div>");
        sb.append("</div>" +
                "</div>" +
                "</body>" +
                "</html>");
        String emailAccount ="notification_test@jegotrip.com.cn";
        String emailPassword ="oGEFbQwfXq4ea6NW";
        try {
            // 指定PDF输出文件的路径
            Path outputFilePath = Paths.get("output3.pdf");
            // 创建PdfWriter实例
            PdfWriter pdfWriter = new PdfWriter(outputFilePath.toFile());
            // 创建ConverterProperties实例，你可以在这里设置转换属性
            ConverterProperties properties = new ConverterProperties();
            final FontProvider fontProvider = new DefaultFontProvider();
            //添加系统字体
            final int ret = fontProvider.addSystemFonts();
            System.out.println("addSystemFonts ret:{}" + ret);
            properties.setFontProvider(fontProvider);
            HtmlConverter.convertToPdf(sb.toString(), pdfWriter, properties);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

}
